<!--
 * @Author: your name
 * @Date: 2021-06-10 14:58:16
 * @LastEditTime: 2021-06-15 16:33:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bazai_ebook\src\views\store\StoreHome.vue
-->
<template>
  <div class="store-home">
    <search-bar />
    <flap-card :data="random" />
    <scroll :top="94" @onScroll="onScroll" ref="scroll">
      <div class="banner-wrapper">
        <div class="banner-img" :style="{backgroundImage:`url(${banner})`}"></div>
      </div>
      <guess-you-like :data="guessYouLike" />
      <recommend class="recommend" :data="recommend" />
      <featured class="featured" :titleText="$t('home.featured')" :btnText="$t('home.seeAll')" :data="featured" />
      <div class="category-list-wrapper" v-for="(item, index) of categoryList" :key="'item' + index">
        <category-book :data="item" />
      </div>
      <category :data="categories" class="categories" />
    </scroll>
  </div>
</template>

<script>
import { storeHomeMixin } from '@/mixins/mixin'
import { home } from '@/api/store'
export default {
  mixins: [storeHomeMixin],
  name: 'StoreHome',
  data () {
    return {
      scrollTop: 94,
      random: [],
      banner: [],
      guessYouLike: [],
      recommend: [],
      featured: [],
      categoryList: [],
      categories: []
    }
  },
  methods: {
    onScroll (offsetY) {
      this.setOffsetY(offsetY)
      if (offsetY > 0) {
        this.scrollTop = 52
      } else {
        this.scrollTop = 94
      }
      this.$refs.scroll.refresh()
    }
  },
  mounted () {
    home().then(res => {
      if (res && res.status === 200) {
        const data = res.data
        const randomIndex = Math.floor(Math.random() * data.random.length)
        this.random = data.random[randomIndex]
        this.banner = data.banner
        this.guessYouLike = data.guessYouLike
        this.recommend = data.recommend
        this.featured = data.featured
        this.categoryList = data.categoryList
        this.categories = data.categories
      }
    })
  }
}
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  .store-home {
    width: 100%;
    height: 100%;
    .banner-wrapper {
      width: 100%;
      padding: px2rem(10);
      box-sizing: border-box;
      .banner-img {
        width: 100%;
        height: px2rem(150);
        background-repeat: no-repeat;
        background-size: 100% 100%;
      }
    }
    .categories,
    .category-list-wrapper,
    .featured,
    .recommend {
      margin-top: px2rem(20);
    }
  }
</style>
